package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoNanJin;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Random;

import static com.gitee.apanlh.web.http.HttpContentType.APPLICATION_JSON;

/**
 * 安心分期-龙岩市企诺API全量15 南京
 *
 * @author 李源聪
 */
@ApiPlan(productId = "307", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5LongYanQiNuoNanJin implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(APPLICATION_JSON);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("secret", "409e3726e46f4910ba80d1f78ae0905b");
            newMap.put("platformId", 101);
            newMap.put("phone", apiDataExchangeParameter.getPhoneMd5());
        }));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        JSONObject result = jsonObject.getJSONObject("result");
        if (result == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(result.getInteger("exits"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(APPLICATION_JSON);
        OrgApplyLongYanQiNuoNanJin orgApplyLongYanQiNuoNanJin = new OrgApplyLongYanQiNuoNanJin();
        //拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        Random random = new Random();
        orgApplyLongYanQiNuoNanJin.setClueId(random.nextLong());
        orgApplyLongYanQiNuoNanJin.setSecret("409e3726e46f4910ba80d1f78ae0905b");
        orgApplyLongYanQiNuoNanJin.setTenantId("1300000000001");
        orgApplyLongYanQiNuoNanJin.setPlatformId("101");
        orgApplyLongYanQiNuoNanJin.setName(apiApplyParameter.getUserName());
        orgApplyLongYanQiNuoNanJin.setPhone(apiApplyParameter.getPhone());
        orgApplyLongYanQiNuoNanJin.setWeixin("无");
        orgApplyLongYanQiNuoNanJin.setQuota(String.valueOf(apiFieldConverter.getLoanAmountInt()));
        // 如： 装修、消费等
        String loanPurposeChoose = ChooseEq.create()
                .when(apiApplyParameter.getLoanPurpose(), "1", "旅游")
                .when(apiApplyParameter.getLoanPurpose(), "2", "教育")
                .when(apiApplyParameter.getLoanPurpose(), "3", "婚庆")
                .when(apiApplyParameter.getLoanPurpose(), "4", "装修")
                .when(apiApplyParameter.getLoanPurpose(), "5", "医疗")
                .when(apiApplyParameter.getLoanPurpose(), "6", "个人消费")
                .end("个人消费");
        orgApplyLongYanQiNuoNanJin.setPurpose(loanPurposeChoose);
        // 有无逾期（0.无，1.有）
        String overdueChoose = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", "好")
                .when(apiApplyParameter.getOverdue(), "2", "不好")
                .when(apiApplyParameter.getOverdue(), "3", "一般")
                .end("未知");
        orgApplyLongYanQiNuoNanJin.setClientLvl(overdueChoose);
        // 房产（1有/0无）
        String realEstateInfoChoose = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "无房产")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "有房不抵押")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "有房可抵押")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "有房产")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "有按揭房产")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "有全款房产")
                .end("无房产");
        orgApplyLongYanQiNuoNanJin.setRealEstate(realEstateInfoChoose);
        orgApplyLongYanQiNuoNanJin.setCity(apiApplyParameter.getCity());

        return JsonUtils.toJson(orgApplyLongYanQiNuoNanJin);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 200);
        if (!code) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
